from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, SubmitField, TextAreaField, SelectField, IntegerField, FileField, DecimalField
from wtforms.validators import DataRequired, Length
from flask_babel import lazy_gettext as _l
from wtforms.fields import MultipleFileField
from wtforms.validators import DataRequired, Email, Length, Regexp, EqualTo, Optional, NumberRange

username_re = r'^[A-Za-z][A-Za-z0-9]{3,31}$'  # 以字母开头，4-32位，仅字母数字


class ProfileForm(FlaskForm):
    username = StringField('Username', validators=[
        DataRequired(), Length(min=4, max=32),
        Regexp(username_re, message="Must start with a letter, letters+numbers only.")
    ])
    email = StringField('Email', validators=[DataRequired(), Email(), Length(max=255)])
    recipient_name = StringField('Recipient Name', validators=[Optional(), Length(max=128)])
    phone = StringField('Phone', validators=[Optional(), Length(max=32),
                       Regexp(r'^[0-9+\-\s()]{6,}$', message="Invalid phone")])
    postal_code = StringField('Postal Code', validators=[Optional(), Length(max=20)])
    shipping_address = TextAreaField('Shipping Address', validators=[Optional(), Length(max=2000)])
    submit = SubmitField('Save')


class RegisterForm(FlaskForm):
    username = StringField('用户名/Username', validators=[
        DataRequired(), Length(min=4, max=32),
        Regexp(username_re, message='仅限字母或字母+数字，且以字母开头（4-32位）')
    ])
    email = StringField('邮箱/Email', validators=[DataRequired(), Email(), Length(max=255)])
    password = PasswordField('密码/Password', validators=[DataRequired(), Length(min=8, max=128)])
    confirm = PasswordField('确认密码/Confirm', validators=[
        DataRequired(), EqualTo('password', message='两次密码不一致')
    ])
    recipient_name = StringField(_l('Recipient Name'),
                                 validators=[DataRequired(), Length(max=64)])
    phone = StringField(_l('Phone'),
                        validators=[DataRequired(),
                                    Regexp(r'^\+?[0-9\s\-()]{6,20}$', message=_l('Invalid phone number.'))])
    postal_code = StringField(_l('Postal Code'),
                              validators=[DataRequired(), Length(min=3, max=20),
                                          Regexp(r'^[A-Za-z0-9\- ]+$', message=_l('Invalid postal code.'))])
    shipping_address = TextAreaField('收货地址/Shipping Address', validators=[
        Length(max=2000)
    ])
    submit = SubmitField('注册')


class LoginForm(FlaskForm):
    username_or_email = StringField('用户名或邮箱', validators=[DataRequired()])
    password = PasswordField('密码/Password', validators=[DataRequired()])
    submit = SubmitField('登录')


class CategoryForm(FlaskForm):
    name = StringField(_l('分类名称（中文）'), validators=[DataRequired(), Length(max=80)])
    name_en = StringField(_l('分类名称（英文）'))
    sort_order = IntegerField(_l('排序'), default=0)
    submit = SubmitField(_l('保存'))


class ProductForm(FlaskForm):
    title = StringField(_l('标题（中文）'), validators=[DataRequired()])
    subtitle = StringField(_l('副标题（中文）'))
    description = TextAreaField(_l('描述（中文）'))
    material = StringField(_l('材质（中文）'))

    title_en = StringField(_l('标题（英文）'))
    subtitle_en = StringField(_l('副标题（英文）'))
    description_en = TextAreaField(_l('描述（英文）'))
    material_en = StringField(_l('材质（英文）'))

    price = IntegerField('价格（分）')
    price_amount = DecimalField('价格', places=2)
    currency = SelectField('币种',
                           choices=[('CNY', 'CNY'), ('USD', 'USD'), ('EUR', 'EUR'), ('HKD', 'HKD'), ('JPY', 'JPY')],
                           default='CNY')
    sku = StringField('SKU')
    material = StringField('材质')
    size_value = DecimalField(_l('尺寸数值'), places=2)
    size_length = DecimalField('长度', places=2)
    size_width = DecimalField('宽度', places=2)
    size_height = DecimalField('高度', places=2)
    size_unit = SelectField(_l('尺寸单位'), choices=[('mm', 'mm'), ('cm', 'cm'), ('in', 'in')], default='mm')
    weight_value = DecimalField(_l('重量数值'), places=2)
    weight_unit = SelectField(_l('重量单位'), choices=[('g', 'g'), ('kg', 'kg'), ('oz', 'oz')], default='g')
    stock = IntegerField('库存', validators=[DataRequired(), NumberRange(min=0)])

    has_certificate = SelectField(_l('是否带认证证书'), choices=[('0', _l('否')), ('1', _l('是'))], default='1')
    can_direct_pay = SelectField(_l('是否可直接支付'),  # ← 新增
                                 choices=[('0', _l('否')), ('1', _l('是'))],
                                 default='1')
    category_id = SelectField('分类', coerce=int)
    is_published = BooleanField('上架', default=True)
    cover_image = FileField('主图')

    min_order_qty = IntegerField(
        '起订量（MOQ）',
        default=1,
        validators=[DataRequired(message='请输入起订量'), NumberRange(min=1, message='起订量至少为 1')]
    )

    gallery = MultipleFileField(_l('更多图片（可多选）'))  # 新增：多图上传
    save = SubmitField('保存')
